package com.qf.controller;

import com.qf.entity.*;
import com.qf.service.StuService;
import com.qf.service.StuSplitOfService;
import com.qf.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * --- 年少有为 ---
 *
 * @author ChenZhuo
 * @date 2025/8/16
 * @desc
 */
@RestController
@Api(tags = "业务层拆解多表联查")
public class StuSplitOfServiceController {
    @Autowired
    private StuSplitOfService stuSplitOfService;
    @Autowired
    private StuService stuService;
    @GetMapping("/stu/splitService/findStuWithClassroom")
    @ApiOperation("分解业务层-查询学生信息以及关联的教室信息")
    public R findStuWithClassrootOfSplitService(){
        List<StuVo> stuVos = new ArrayList<>();
        List<Stu> all = stuService.findAll();
        for (Stu stu : all) {
            StuVo stuVo = new StuVo();
            Classroom classroom=stuSplitOfService.findClassroomByCid(stu.getCid());
            BeanUtils.copyProperties(stu,stuVo);
            stuVo.setClassroom(classroom);
            stuVos.add(stuVo);
        }

        return R.success(stuVos);
    }
    @GetMapping("/stu/splitService/findStuWithSubject")
    @ApiOperation("分解业务层-查询一个学生信息以及所学习的所有学科信息")
    public R findStuWithSubject(){
        ArrayList<StuVoWithSubject> stuVoWithSubjects = new ArrayList<>();
        List<Stu> stus = stuService.findAll();
        for (Stu stu : stus) {
            StuVoWithSubject stuVoWithSubject = new StuVoWithSubject();
            ArrayList<Stusub> stusubs=stuService.findStuSub(stu.getId());
            ArrayList<Subject> subjects = new ArrayList<>();
            Subject subject = new Subject();
            for (Stusub stusub : stusubs) {
                subject=stuService.findSubjectBySubId(stusub.getSubId());
                subjects.add((subject));
            }
            BeanUtils.copyProperties(stu,stuVoWithSubject);
            stuVoWithSubject.setSubject(subjects);
            stuVoWithSubjects.add(stuVoWithSubject);
        }

        return R.success(stuVoWithSubjects);
    }
}
